//
// Created by Semir on 2020/5/9.
//

#include <iostream>

using namespace std;

struct TreeLinkNode {
    int val;
    struct TreeLinkNode *left;
    struct TreeLinkNode *right;
    struct TreeLinkNode *next;
    explicit TreeLinkNode(int x) : val(x), left(nullptr), right(nullptr), next(nullptr) {}
};

TreeLinkNode* GetNext(TreeLinkNode* pNode) {
    if (pNode == nullptr) {
        return nullptr;
    }
    TreeLinkNode* cNode = pNode;
    if (pNode->right) {
        cNode = cNode->right;
        if (cNode->left) {
            while (cNode->left) {
                cNode = cNode->left;
            }
        }
        return cNode;
    }
    if (!pNode->next) {
        return nullptr;
    }
    if (pNode->next->left == pNode) {
        return pNode->next;
    }
    cNode = pNode->next;
    while (cNode->next && cNode->next->right == cNode) {
        cNode = cNode->next;
    }
    return cNode->next;
}

int main() {

    return 0;
}
